import ArchitectureStyles.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import Utils.FileReader;
public class UI {
    private JFrame frame;
    private JComboBox<String> architectureComboBox;
    private JTextArea resultArea;

    public UI() {
        frame = new JFrame("经典软件体系结构教学软件");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 650);
        frame.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        String[] architectures = {"主程序-子程序", "面向对象", "事件系统", "管道-过滤器"};
        architectureComboBox = new JComboBox<>(architectures);
        JButton processButton = new JButton("处理文件");

        panel.add(architectureComboBox);
        panel.add(processButton);

        resultArea = new JTextArea();
        resultArea.setEditable(false);

        frame.add(panel, BorderLayout.NORTH);
        frame.add(new JScrollPane(resultArea), BorderLayout.CENTER);

        processButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                processFile((String) architectureComboBox.getSelectedItem());
            }
        });
    }

    private void processFile(String architecture) {
        String inputFilePath = "resources/input.txt";
        List<String> lines = null;
        try {
            lines = Files.readAllLines(Paths.get(inputFilePath));
        } catch (IOException ex) {
            ex.printStackTrace();
            resultArea.setText("无法读取文件");
            return;
        }

        String result = "";
        switch (architecture) {
            case "主程序-子程序":
                result = MainSubroutine.principle() +"\n\n原文：\n"+ FileReader.readFile(lines)+"\n\n处理结果：\n"+ MainSubroutine.process(lines);
                break;
            case "面向对象":
                result = ObjectOriented.principle() +"\n\n原文：\n"+ FileReader.readFile(lines)+"\n\n处理结果：\n"+ObjectOriented.process(lines);
                break;
            case "事件系统":
                result = EventDriven.principle() +"\n\n原文：\n"+ FileReader.readFile(lines)+"\n\n处理结果：\n"+EventDriven.process(lines);
                break;
            case "管道-过滤器":
                result = PipeFilter.principle() +"\n\n原文：\n"+ FileReader.readFile(lines)+"\n\n处理结果：\n"+PipeFilter.process(lines);
                break;
        }

        resultArea.setText(result);
    }

    public void displayUI() {
        frame.setVisible(true);
    }
}